home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / irit / plotter.irt < prev    next >
Encoding:
Text File  |  1993-12-30  |  3.6 KB  |  131 lines

  1. #
  2. # A package to plot explicit curves and surfaces as polylines.
  3. #
  4. #
  5. #            Gershon Elber, October 1993.
  6. #
  7.  
  8. #
  9. # The functions to plot. Get x or x,y and returns y or z values.
  10. #
  11. plotfn2d = function(x):
  12.     return = x^3 - 5*x^2 + 4*x - 5;
  13.  
  14. plotfn3d = function(x, y):
  15.     return = x*x - y*y;
  16.  
  17. echosrc = false;
  18.  
  19. #
  20. # Computes a polyline out of the plotfn2d function from paramter values
  21. # min to max in n steps.
  22. #
  23. plotfunc2d2poly = function(min, max, n):lst:t:
  24.     lst = nil():
  25.     for (t = min, (max - min) / (n - 1), max,
  26.     snoc(vector(t, plotfn2d(t), 0.0), lst)):
  27.     return = poly(lst, true);
  28.  
  29. #
  30. # Reposition the 2d polyline in the [-1..1] xy domain and plot it.
  31. #
  32. plotfunc2d = function(minx, maxx, n):pl:miny:maxy:i:v:tr:ax:sv:
  33.     pl = plotfunc2d2poly(minx, maxx, n):
  34.     color(pl, yellow):
  35.     attrib(pl, "width", 0.05):
  36.  
  37.     # find the Y domain of the function.
  38.     miny = 1e6:
  39.     maxy = -1e6:
  40.     for (i = 0, 1, 2, miny = miny+i):
  41.     return = pl:
  42.  
  43.     for (i = 0, 1, listsize(pl) - 1,
  44.     v = coord(pl, i):
  45.     if (coord(v, 1) > maxy, maxy = coord(v, 1)):
  46.     if (coord(v, 1) < miny, miny = coord(v, 1))):
  47.  
  48.     # Make the axes
  49.     ax = poly(list(vector(min(minx, 0), 0, 0),
  50.                vector(max(maxx, 0), 0, 0)),
  51.           true) +
  52.      poly(list(vector(0, min(miny, 0), 0),
  53.            vector(0, max(maxy, 0), 0)),
  54.           true):
  55.     color(ax, red):
  56.     attrib(ax, "width", 0.02):
  57.  
  58.     # Map and display the polyline to the [-1..1] domain in both x and y.
  59.     tr = trans(vector(-(minx + maxx) / 2, -(miny + maxy) / 2, 0)) *
  60.      scale(vector(2 / (maxx - minx), 2 / (maxy - miny), 0)):
  61.     sv = view_mat:
  62.     view_mat = rotx(0):
  63.     viewobj(list(view_mat, return = list(pl, ax) * tr)):
  64.     printf("XDomain = [%lf %lf], YDomain = [%lf %lf]\\n",
  65.        list(minx, maxx, miny, maxy)):
  66.     view_mat = sv;
  67.  
  68. #
  69. # Computes polylines out of the plotfn3d function from parameter values
  70. # minx/y to maxx/y in n steps and m isolines for each size.
  71. #
  72. plotfunc3d2poly = function(minx, maxx, miny, maxy, n, m):lst:x:y:
  73.     return = 0:
  74.     for (x = minx, (maxx - minx) / (m - 1), maxx,
  75.     lst = nil():
  76.     for (y = miny, (maxy - miny) / (n - 1), maxy,
  77.         snoc(vector(x, y, plotfn3d(x, y)), lst)):
  78.     if (thisobj(return) == numeric_type,
  79.         return = poly(lst, true),
  80.         return = return + poly(lst, true))):
  81.     for (y = miny, (maxy - miny) / (m - 1), maxy,
  82.     lst = nil():
  83.     for (x = minx, (maxx - minx) / (n - 1), maxx,
  84.         snoc(vector(x, y, plotfn3d(x, y)), lst)):
  85.     return = return + poly(lst, true));
  86.  
  87. #
  88. # Plot the 3d polylines in their original domain.
  89. #
  90. plotfunc3d = function(minx, maxx, miny, maxy, n, m):pl:minz:maxz:v:p:ax:i:j:
  91.     pl = plotfunc3d2poly(minx, maxx, miny, maxy, n, m):
  92.     color(pl, yellow):
  93.     attrib(pl, "width", 0.05):
  94.  
  95.     # find the Y domain of the function.
  96.     minz = 1e6:
  97.     maxz = -1e6:
  98.     for (i = 0, 1, listsize(pl) - 1,
  99.         p = coord(pl, i):
  100.     for (j = 0, 1, listsize(p) - 1,
  101.         v = coord(p, i):
  102.         if (coord(v, 2) > maxz, maxz = coord(v, 2)):
  103.         if (coord(v, 2) < minz, minz = coord(v, 2)))):
  104.  
  105.     # Make the axes
  106.     ax = poly(list(vector(min(minx, 0), 0, 0),
  107.                vector(max(maxx, 0), 0, 0)),
  108.           true) +
  109.      poly(list(vector(0, min(miny, 0), 0),
  110.            vector(0, max(maxy, 0), 0)),
  111.           true) +
  112.      poly(list(vector(0, 0, min(minz, 0)),
  113.            vector(0, 0, max(maxz, 0))),
  114.           true):
  115.     color(ax, red):
  116.     attrib(ax, "width", 0.02):
  117.  
  118.     # Plot the polylines to their original domain.
  119.     viewobj(return = list(pl, ax)):
  120.     printf("XDomain = [%lf %lf], YDomain = [%lf %lf], ZDomain = [%lf %lf]\\n",
  121.        list(minx, maxx, miny, maxy, minz, maxz));
  122.  
  123. echosrc = true;
  124.  
  125. viewclear();
  126. fn = plotfunc2d(-1, 5, 50);
  127. pause(0);
  128.  
  129. viewclear();
  130. fn = plotfunc3d(-1, 1, -1, 1, 30, 10);
  131. pause(0);